﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Threading;
using Test;
using System.Text.RegularExpressions;
using System.Transactions;
namespace EvLog {
	static class EvLogReturns {
		public static DAL local=new DAL();
		static void MaiXn() {
			List<string> MachineNames=new List<string>();
			//MachineNames.AddRange(Mixer.Combine("pon-wpwb0{0}-cl0{1}","3","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wpas10-cl0{0}","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wprf0{0}-cl0{1}","1,2,3,4","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wprf0{0}-cl0{1}","1,2","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wpwb0{0}-cl0{1}","1,2,3","1"));
			//MachineNames.AddRange(Mixer.Combine("pon-wpsq{0}-cl0{1}","01,02,03,04,06,07,08,09,10,11,12,13,15,16","1,2"));
			MachineNames.AddRange(Mixer.CombineEx("pon-wpct{0:00}","51-75"));
			//MachineNames.AddRange(Mixer.Combine("pon-wpmq01-cl0{0}","1,2"));
			//MachineNames.Add("PON-WPAS10-cl01");
			//MachineNames.Add("PON-WPAS10-cl02");
			//MachineNames.Add("PON-WPsq10-cl01");
			//MachineNames.Add("PON-WPsq10-cl01");
			local.NonQry("truncate table ElogReturns");
			List<Thread> thLst=new List<Thread>();
			foreach(string machine in MachineNames)
				thLst.Add(new Thread(new LogReaderRet(machine).Read));
			Stopwatch sw=Stopwatch.StartNew();
			foreach(Thread th in thLst)
				th.Start();
			foreach(Thread th in thLst)
				th.Join();
			Console.WriteLine("THE END");
			Console.WriteLine(sw.Elapsed);
		}
	}
	class LogReaderRet {
		string format="insert into ElogReturns(cat,eType,machine,src,eTime,usr,msg,log,data)values('{0}','{1}','{2}','{3}',convert(datetime,'{4}',120),'{5}','{6}','{7}','{8}')";
		string _machine;
		public LogReaderRet(string machine) { _machine=machine; }
		public void Read() {
			EventLog[] logs=EventLog.GetEventLogs(_machine);
			foreach(EventLog log in logs)
				if(log.Log.ToLower()=="application")
					for(int i=log.Entries.Count-1;i>=0;i--)
					//foreach(EventLogEntry ent in log.Entries)
					{
						EventLogEntry ent=default(EventLogEntry);
						try
						{
							ent=log.Entries[i];
						}
						catch(Exception ex) { Console.WriteLine(i+":"+ex.Message); continue; }
						//if(ent.TimeWritten.Date.AddDays(1)<DateTime.Now.Date)
						//if(ent.TimeWritten<DateTime.Now.AddHours(-20))
						if(ent.TimeWritten<DateTime.Now.AddDays(-4))
							break;
						//if(ent.TimeWritten>new DateTime(2013,02,27,15,0,0))
						//  continue;
						//if(ent.TimeWritten<new DateTime(2013,02,27,12,0,0))
						//  break;
						string data="";
						if(ent.Source=="Next.Whs.Returns Logging")
							if(ent.Message.ToLower().Contains("invalid scan"))
							{
								//if(ent.Message.Contains("does not have an implementation"))
								//if(ent.Source=="Next.Whs.ReturnsService2")
								//if(ent.Source=="Next.Whs.DirectoryLocating.RF")
								//if(ent.Message.Contains("The timeout period elapsed prior to completion of the operation or the server is not responding."))
								//if(ent.Message.ToLower().Contains("timeout"))


								data=Regex.Match(ent.Message,"NEXT-PLC\\\\(.+?)\\r\\n",RegexOptions.Singleline).Groups[1].Value;
								{
									//Console.WriteLine(ent.Message);
									EvLog.local.NonQry(format,ent.Category,ent.EntryType,ent.MachineName,ent.Source,ent.TimeGenerated.ToString("yyyy-MM-dd HH:mm:ss"),ent.UserName,ent.Message.Replace("'","''"),log.Log.ToLower(),data);
								}
							}
					}
			Console.WriteLine(_machine);
		}
	}
}
/*
drop table elog
create table elog(
id int identity,
cat varchar(255),
eType varchar(25),
machine varchar(50),
log varchar(30),
src varchar(255),
eTime datetime,
usr varchar(50),
msg varchar(max),
constraint pk_elog_id primary key(id)
)
create index ix_elog_eTime on elog(eTime)
*/
